package com.linhui.rpc.server.impl;

import com.linhui.rpc.server.HttpServer;
import com.linhui.rpc.server.HttpServerHandler;
import io.vertx.core.Vertx;


/**
 * @author: 临晖
 * @since: 2024-05-10
 */
public class VertxHttpServer implements HttpServer {


    public void doStart(int port) {
        //创建vertx对象
        Vertx vertx = Vertx.vertx();
        //通过vertx对象获取httpserver
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();
        server.requestHandler(new HttpServerHandler());

        //监听端口，并处理请求
        //server.requestHandler(req -> {
        //    //处理请求
        //
        //    //发送http响应
        //    req.response()
        //            .putHeader("content-type", "text/plain")
        //            .end("Hello World!");
        //});

        server.listen(port, "localhost", res -> {
            if (res.succeeded()) {
                System.out.println("Server is now listening on actual port: " + server.actualPort());
            } else {
                System.out.println("Failed to bind!");
            }
        });
    }
}
